package com.gdk.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.gdk.service.ArticleService;
import com.gdk.service.CategoryService;
import com.gdk.service.GoodsService;
import com.gdk.service.LoginService;
import com.gdk.service.OrderService;
import com.gdk.service.QyService;
import com.gdk.service.TreeService;
import com.gdk.service.UserService;
import com.gdk.utils.HUtil;
import com.gdk.utils.XVDateUtils;
import com.gdk.wx.utils.ClientCustomSSL;


/**
 *订单controller
 * */

@Controller
public class OrderController {
	
	@Autowired
    private OrderService orderService;
	@Autowired
    private TreeService treeService;
	@Autowired
    private QyService qyService;
	@Autowired
    private UserService userService;
	@Autowired
    private GoodsService goodsService;
	/**
	 * 查询所有订单
	 * */
	@RequestMapping("/selectAllOrder.do")
	public String selectAllOrder(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allOrder = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = orderService.selectAllOrder(map).size();
		
		map.put("allOrderItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/order/order-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allOrder = orderService.selectAllOrder(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
		//Gson gson = new Gson();
		for (Map<String, Object> map2 : allOrder) {
			if(map2.get("pay_time") != null) {
				int p = Integer.valueOf(map2.get("pay_time").toString());
				if(p != 0) {
					map2.put("pay_time", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("created_at") != null) {
				int p = Integer.valueOf(map2.get("created_at").toString());
				if(p != 0) {
					map2.put("created_at", XVDateUtils.MillisFormat(p));	
				}
			}
			//Map<String,Object> sp = new HashMap<String,Object>();
			//规格
			/*if(map2.get("o_gg") != null && !"".equals(map2.get("o_gg").toString())) {
				sp = gson.fromJson(map2.get("o_gg").toString(), sp.getClass());
			}
			map2.put("sp", sp);*/
		}
		
		
		
		request.setAttribute("allOrder",allOrder);
		return "jsp/order/order-list";
	}
	
	/**
	 *修改前查询订单详情
	 * */
	@RequestMapping("/preSelectOrderDetails.do")
	public String preSelectUserDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> orderDetails = orderService.selectOrderDetails(map);
		
		request.setAttribute("orderDetails",orderDetails);
		return "jsp/order/order-edit";
	}
	
	/**
	 * 修改订单
	 * */
	@ResponseBody
	@RequestMapping("/updateOrder.do")
	public int updateOrder(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("订单 : "+map);
		int shop = orderService.updateOrderDetails(map);
		return shop;
	}

	/**
	 * 查询可分配的树
	 * */
	@RequestMapping("/preBindTreeSelectAllTree.do")
	public String preBindTreeSelectAllTree(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		map.put("nowtime", XVDateUtils.getCurrenTimeMillis());
		map.put("ft_status", 1);
		List<Map<String, Object>> allTreeByTerm = treeService.selectAllTreeByTerm(map);
		System.out.println("可分配的树allTreeByTerm   : "+allTreeByTerm);
		request.setAttribute("allTreeByTerm", allTreeByTerm);
		request.setAttribute("o_id", map.get("o_id"));
		return "jsp/order/order-tree-bind";
	}
	/**
	 * 绑定树
	 * */
	@ResponseBody
	@RequestMapping("/bindTreeSelectAllTree.do")
	public int bindTreeSelectAllTree(@RequestParam Map<String,Object> map, HttpServletRequest request) {

		System.out.println("绑定树  : "+map);
		
		//查询订单详情
		Map<String, Object> orderDetails = orderService.selectOrderDetails(map);
		
		int o_flag = Integer.valueOf(orderDetails.get("o_flag").toString());
	//	Date date = new Date();
	//	Calendar cal = Calendar.getInstance();
		
	//	cal.setTime(date);//设置起时间
	//	cal.add(Calendar.YEAR, 1);//增加一年
	//	long inMillis = cal.getTimeInMillis();
	//	int ft_endtime = (int) (inMillis/1000);
		
		int ft_endtime = XVDateUtils.stringDateToMillis(map.get("ft_endtime").toString());
		
		//System.out.println(inMillis + "====" +ft_endtime);
		//绑定树
		Map<String,Object> orMap = new HashMap<String,Object>();
		orMap.put("or_end_time", ft_endtime);
		orMap.put("or_c_time", XVDateUtils.getCurrenTimeMillis());
		orMap.put("o_id", orderDetails.get("o_id"));
		orMap.put("u_id", orderDetails.get("u_id"));
		orMap.put("rg_id", orderDetails.get("goods_id"));
		orMap.put("rgp_id", orderDetails.get("gphoto_id"));
		orMap.put("or_status", 1);
		orMap.put("o_flag", o_flag);
		
		String pre_ft = map.get("ft_id").toString();
		
		
		//修改树的到期时间
		Map<String,Object> treeMap = new HashMap<String,Object>();
		treeMap.put("ft_endtime", ft_endtime);
		

		int tree =0;
		if(pre_ft.contains(",")) {
			//分割树 ,
			String[] s_tree = pre_ft.split(",");
			for (String string : s_tree) {
				if(!string.equals("")) {
					int ft_id= Integer.valueOf(string);
					orMap.put("ft_id", ft_id);
					treeMap.put("ft_id", ft_id);
					
					int status = treeService.updateTreeDetails(treeMap);
					if(status == 1) {
						tree = orderService.bindTree(orMap);
					}
				}
			}
		
		}else {
			treeMap.put("ft_id",  Integer.valueOf(map.get("ft_id").toString()));
			int status = treeService.updateTreeDetails(treeMap);
			if(status == 1) {
				orMap.put("ft_id", Integer.valueOf(map.get("ft_id").toString()));
				tree = orderService.bindTree(orMap);
			}
		}
		//修改订单状态
		int st = 0;
		if(tree ==  1) {
			Map<String,Object> omap = new HashMap<String,Object>();
			omap.put("os_id", 4);
			omap.put("o_id", orderDetails.get("o_id"));
			st = orderService.updateOrderDetails(omap);
		}
		return st;
	}
	
	
	/**
	 * 绑定权益前查询
	 * */

	@RequestMapping("/preBindQyByOrder.do")
	public String preBindQyByOrder(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		//查询此订单是否已绑定
		List<Map<String, Object>> allQyByOrder = qyService.selectAllQyByOrder(map);
		
		//绑定过
		if(allQyByOrder.size() > 0) {
			request.setAttribute("qy_status", 1);
			 
		}
		
		 List<Map<String, Object>> allQy = qyService.selectAllQy();
		 request.setAttribute("allQy", allQy);
		 
		 request.setAttribute("map", map);
		return "jsp/order/qy-bind";
	}
	
	/**
	 * 绑定权益
	 * */
	@ResponseBody
	@RequestMapping("/bindQyByOrder.do")
	public int bindQyByOrder(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("打印看看  : "+map);
		
		//遍历fa_id
		String all_fa_id = map.get("fa_id").toString();
		
		Map<String,Object> qyMap = new HashMap<String,Object>();
		qyMap.put("u_id", map.get("u_id"));
		qyMap.put("o_id", map.get("o_id"));
		
		int qy = 0;
		
		if(all_fa_id.contains(",")) {
			String[] split = all_fa_id.split(",");
			for (String str : split) {
				if(!str.equals("")) {
					int fa_id = Integer.valueOf(str);
					qyMap.put("fa_id", fa_id);
					//查询fa_id的详情
					Map<String, Object> qyDetails = qyService.selectQyDetails(qyMap);
					
					qyMap.put("qy_sumcount", qyDetails.get("fa_count"));
					qyMap.put("qy_count", qyDetails.get("fa_count"));
					qyMap.put("qy_type", 0);
					qyMap.put("qy_time",XVDateUtils.getCurrenTimeMillis());
					//绑定
					qy = qyService.bindQy(qyMap);
					
				}
			}
			
		}else {
			//查询fa_id的详情
			qyMap.put("fa_id", map.get("fa_id"));
			//查询fa_id的详情
			Map<String, Object> qyDetails = qyService.selectQyDetails(qyMap);
			
			qyMap.put("qy_sumcount", qyDetails.get("fa_count"));
			qyMap.put("qy_count", qyDetails.get("fa_count"));
			qyMap.put("qy_type", 0);
			qyMap.put("qy_time",XVDateUtils.getCurrenTimeMillis());
			//绑定
			qy = qyService.bindQy(qyMap);
			
		}
		//int bindQy = qyService.bindQy(map);
		return qy;
	}
	
	/**
	 * 查询所有订单评价
	 * */
	@RequestMapping("/selectAllOrderComment.do")
	public String selectAllOrderComment(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allOrderComment = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = orderService.selectAllOrderComment(map).size();
		
		map.put("allOrderCommentItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/order/order-comment-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allOrderComment = orderService.selectAllOrderComment(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
		//Gson gson = new Gson();
		for (Map<String, Object> map2 : allOrderComment) {
			if(map2.get("pj_time") != null) {
				int p = Integer.valueOf(map2.get("pj_time").toString());
				if(p != 0) {
					map2.put("pj_time", XVDateUtils.MillisFormat(p));	
				}
			}
			//Map<String,Object> sp = new HashMap<String,Object>();
			//规格
			/*if(map2.get("o_gg") != null && !"".equals(map2.get("o_gg").toString())) {
				sp = gson.fromJson(map2.get("o_gg").toString(), sp.getClass());
			}
			map2.put("sp", sp);*/
		}
		
		
		
		request.setAttribute("allOrderComment",allOrderComment);
		return "jsp/order/order-comment-list";
	}
	
	/**
	 * 查询所有订单评价
	 * */
	@RequestMapping("/selectOrderCommentDetails.do")
	public String selectOrderCommentDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		request.setAttribute("orderCommentDetails", orderService.selectOrderCommentDetails(map));
		return "jsp/order/order-comment-detail";
	}
	/**
	 * 修改订单评价状态
	 * */
	@ResponseBody
	@RequestMapping("/updateOrderCommentDetails.do")
	public int updateOrderCommentDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("审核 map : "+map);
		int score = 0;
		int re_status = 0;
		if(map.get("score") !=null && !"".equals(map.get("score").toString())) {
			score = (int)Double.parseDouble((map.get("score").toString()));
			System.out.println("score: "+score);
			map.put("score", score);
		}
		
		if(score == 0) {
			re_status = orderService.updateOrderCommentDetails(map);
		}else {
			//有积分的操作
			re_status = orderService.updateOrderCommentDetails(map);
			
			Map<String, Object> commentDetails = orderService.selectOrderCommentDetails(map);
			
			double z_integral = 0;
			//查询出用户最新积分
			List<Map<String, Object>> integralByUser = userService.selectIntegralByUser(commentDetails);
			if(integralByUser.size()>0) {
				z_integral = Double.parseDouble(integralByUser.get(0).get("ir_sum").toString());
			}
			//赠送积分
			Map<String,Object> imap = new HashMap<String,Object>();
			imap.put("u_id", commentDetails.get("u_id"));
			imap.put("ir_sum", z_integral+score);
			imap.put("ir_fraction",score);
			imap.put("ir_status",0);
			imap.put("ir_type",4);
			imap.put("ir_time",XVDateUtils.getCurrenTimeMillis());
			imap.put("o_id", commentDetails.get("o_id"));
			userService.addsIntegralByUser(imap);
			
		}
		
		return re_status;
	}
	

	/**
	 * 退款操作
	 */
	@ResponseBody
	@RequestMapping("/refundPay.do")
	public int refundPay(@RequestParam Map<String, Object> map, HttpServletRequest request) {

		System.out.println("退款操作的map :" + map);
		int re_status = 0;
		/*
		 * 查询此订单信息
		 * */
		
		
		Map<String, Object> orderDetails = orderService.selectOrderDetails(map);
		
		//查询此订单实际支付金额,并查询出使用的积分和余额进行返还
		double totalFee = Double.parseDouble(orderDetails.get("o_actual_money").toString());
		
		double o_integral = Double.parseDouble(orderDetails.get("o_integral").toString());
		double o_commission = Double.parseDouble(orderDetails.get("o_commission").toString());
		
		double z_integral = 0;
		double z_commission = 0;
		//赠送的积分
		double zs_integral = 0;
		//查询出用户最新积分
		List<Map<String, Object>> integralByUser = userService.selectIntegralByUser(orderDetails);
		if(integralByUser.size()>0) {
			z_integral = Double.parseDouble(integralByUser.get(0).get("ir_sum").toString());
		}
		//查询出用户最新佣金
		List<Map<String, Object>> commissionByUser = userService.selectCommissionByUser(orderDetails);
		if(commissionByUser.size()>0) {
			z_commission = Double.parseDouble(commissionByUser.get(0).get("c_sum").toString());
		}
		
		//查询指定订单奖励的积分
		Map<String,Object> omap = new HashMap<String,Object>();
		omap.put("o_id", map.get("o_id"));
		omap.put("ir_status",0);
		List<Map<String, Object>> integralByOrder = userService.selectIntegralByOrder(omap);
		if(integralByOrder.size()>0) {
			zs_integral = Double.parseDouble(integralByOrder.get(0).get("ir_fraction").toString());
		}
		
		
		
		//查询商品信息,增加库存
		Map<String,Object> gmap = new HashMap<String,Object>();
		gmap.put("rg_id", orderDetails.get("goods_id"));
		
		Map<String, Object> goodsDetails = goodsService.selectGoodsDetails(gmap);
		
		int o_amount = Integer.valueOf(orderDetails.get("o_amount").toString());
		
		int rg_stock  = Integer.valueOf(goodsDetails.get("rg_stock").toString());
		int rg_actual_sold  = Integer.valueOf(goodsDetails.get("rg_actual_sold").toString());
	
		gmap.put("rg_stock",rg_stock+o_amount);
		gmap.put("rg_actual_sold",rg_actual_sold - o_amount );
		
		
		//查询此订单有无赠送积分
		
		
		
		if(totalFee > 0) {
			//使用微信支付
			
		
			
			// 执行退款操作,并减去推荐上级的佣金,记录退款金额到支付表
	
			/*
			 * 1.调用方法,执行微信退款
			 */
			String merchantNumber = (String) orderDetails.get("o_out_trade_no");
			// String wxTransactionNumber = "4200000218201901097243772476";
			// 本次金额
			double refund_fee = Double.parseDouble(orderDetails.get("o_actual_money").toString());
			// 申请退款
			Object setUrl = ClientCustomSSL.setUrl(merchantNumber, totalFee, refund_fee);
			System.out.println("result : " + setUrl.toString());
			Gson gson = new Gson();
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap = gson.fromJson(setUrl.toString(), resultMap.getClass());
			System.out.println("resultMap : " + resultMap);
			String msg = (String) resultMap.get("msg");
	//		/System.out.println("msg : "+msg);
			/*
			 * 2.调用方法,微信退款成功保存到订单表,并减掉上级佣金
			 */
			if (msg != null && msg.equals("success")) {
				// 保存退款信息
				System.out.println("退款成功");
				
				//修改订单状态
				Map<String,Object> ormap = new HashMap<String,Object>();
				ormap.put("o_id", map.get("o_id"));
				ormap.put("os_id", 6);
				orderService.updateOrderDetails(ormap);
				
				//返还商品库存
				goodsService.updateGoodsDetails(gmap);
				goodsDetails.put("rg_stock",rg_stock+o_amount);
				goodsDetails.put("rg_actual_sold",rg_actual_sold - o_amount );
				
				if(goodsDetails.get("rg_atime") !=null && !"".equals(goodsDetails.get("rg_atime").toString())) {
					goodsDetails.put("rg_atime", XVDateUtils.stringDateToMillis(goodsDetails.get("rg_atime").toString()));
					goodsDetails.put("rg_etime", XVDateUtils.stringDateToMillis(goodsDetails.get("rg_etime").toString()));
					
				}
				
				goodsService.addGoodsPhoto(goodsDetails);
				
				Map<String,Object> kcmap = new HashMap<String,Object>();
				kcmap.put("rg_id", orderDetails.get("goods_id"));
				kcmap.put("gt_count",o_amount);
				kcmap.put("gt_type",1);
				kcmap.put("time",XVDateUtils.getCurrenTimeMillis());
				kcmap.put("o_id", map.get("o_id"));
				goodsService.addGoodsStock(kcmap);
				
				
				//查询有无使用积分和余额
				if(o_integral > 0) {
					//返还积分
					Map<String,Object> imap = new HashMap<String,Object>();
					imap.put("u_id", orderDetails.get("u_id"));
					imap.put("ir_sum", z_integral+o_integral-zs_integral);
					imap.put("ir_fraction",o_integral-zs_integral);
					imap.put("ir_status",0);
					imap.put("ir_type",3);
					imap.put("ir_time",XVDateUtils.getCurrenTimeMillis());
					imap.put("o_id", map.get("o_id"));
					userService.addsIntegralByUser(imap);
				}
				if(o_commission > 0) {
					//返还佣金
					Map<String,Object> cmap = new HashMap<String,Object>();
					cmap.put("u_id", orderDetails.get("u_id"));
					cmap.put("c_sum", z_commission+o_commission);
					cmap.put("c_commission",o_commission);
					cmap.put("c_status",0);
					cmap.put("c_time",XVDateUtils.getCurrenTimeMillis());
					cmap.put("o_id", map.get("o_id"));
					userService.addCommissionByUser(cmap);
				}
				
				
				
				
				
				
				
				
				
				
			} else if (msg != null && msg.equals("累计退款金额大于支付金额")) {
				System.out.println("累计退款金额大于支付金额");
				re_status = 3;
				return re_status;
			} else if (msg != null && msg.equals("基本账户余额不足，请充值后重新发起")) {
				System.out.println("基本账户余额不足，请充值后重新发起");
				re_status = 4;
				return re_status;
			} else if (msg != null && !msg.equals("success")) {
				// System.out.println("判断 : "+msg.equals("not exist"));
				System.out.println("退款失败");
				re_status = 0;
				return re_status;
			}
			re_status = 1;
			return re_status;
		}else {
			//未支付现金,返还积分和余额
			if(o_integral > 0) {
				//返还积分
				Map<String,Object> imap = new HashMap<String,Object>();
				imap.put("u_id", orderDetails.get("u_id"));
				imap.put("ir_sum", z_integral+o_integral-zs_integral);
				imap.put("ir_fraction",o_integral-zs_integral);
				imap.put("ir_status",0);
				imap.put("ir_type",3);
				imap.put("ir_time",XVDateUtils.getCurrenTimeMillis());
				imap.put("o_id", map.get("o_id"));
				re_status = userService.addsIntegralByUser(imap);
			}
			if(o_commission > 0) {
				//返还佣金
				Map<String,Object> cmap = new HashMap<String,Object>();
				cmap.put("u_id", orderDetails.get("u_id"));
				cmap.put("c_sum", z_commission+o_commission);
				cmap.put("c_commission",o_commission);
				cmap.put("c_status",0);
				cmap.put("c_time",XVDateUtils.getCurrenTimeMillis());
				cmap.put("o_id", map.get("o_id"));
				re_status = userService.addCommissionByUser(cmap);
			}
			//修改订单状态
			Map<String,Object> ormap = new HashMap<String,Object>();
			ormap.put("o_id", map.get("o_id"));
			ormap.put("os_id", 6);
			orderService.updateOrderDetails(ormap);
			//返还商品库存
			goodsService.updateGoodsDetails(gmap);
			goodsDetails.put("rg_stock",rg_stock+o_amount);
			goodsDetails.put("rg_actual_sold",rg_actual_sold - o_amount );
			//添加快照
			if(goodsDetails.get("rg_atime") !=null && !"".equals(goodsDetails.get("rg_atime").toString())) {
				goodsDetails.put("rg_atime", XVDateUtils.stringDateToMillis(goodsDetails.get("rg_atime").toString()));
				goodsDetails.put("rg_etime", XVDateUtils.stringDateToMillis(goodsDetails.get("rg_etime").toString()));
				
			}
			
			goodsDetails.put("new_updatetime", XVDateUtils.getCurrenTimeMillis());
			goodsService.addGoodsPhoto(goodsDetails);
			
			Map<String,Object> kcmap = new HashMap<String,Object>();
			kcmap.put("rg_id", orderDetails.get("goods_id"));
			kcmap.put("gt_count",o_amount);
			kcmap.put("gt_type",1);
			kcmap.put("time",XVDateUtils.getCurrenTimeMillis());
			kcmap.put("o_id", map.get("o_id"));
			goodsService.addGoodsStock(kcmap);
			
			return re_status;
		}
	}
	
	/**
	 *发货信息前查询订单详情
	 * */
	@RequestMapping("/preFahuoOrderDetails.do")
	public String preFahuoOrderDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> orderDetails = orderService.selectOrderDetails(map);
		
		request.setAttribute("orderDetails",orderDetails);
		return "jsp/order/order-express-edit";
	}
	
	/**
	 *修改发货信息前查询订单详情
	 * */
	@RequestMapping("/preEditFahuoOrderDetails.do")
	public String preEditFahuoOrderDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> orderDetails = orderService.selectOrderDetails(map);
		
		request.setAttribute("orderDetails",orderDetails);
		return "jsp/order/order-express-edit";
	}
	/**
	 * 查询订单详情
	 * */
	@RequestMapping("/selectOrderDetails.do")
	public String selectOrderDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		List<Map<String, Object>> allBindTicketByOrder = orderService.selectAllBindTicketByOrder(map);

		request.setAttribute("allBindTicketByOrder",allBindTicketByOrder);
		return "jsp/order/order-details";
	}
	
	
	
	/**
	 * 退款操作
	 */
	@ResponseBody
	@RequestMapping("/refundOrderPay.do")
	public int refundOrderPay(@RequestParam Map<String, Object> map, HttpServletRequest request) {
		System.out.println("退款操作的map :" + map);
		
		int re_status = 0;
		/*
		 * 查询此订单信息
		 * */
		//查询出订单详情
		List<Map<String, Object>> orderDetailsList = orderService.selectAllBindTicketByOrder(map);
		
		Map<String, Object> orderDetails = orderDetailsList.get(0);
		
		
		
		//查询此订单实际支付金额 
		double order_amount_total = Double.parseDouble(orderDetails.get("order_amount_total").toString());
		//使用微信支付
		// 执行退款操作,并减去推荐上级的佣金,记录退款金额到支付表

		/*
		 * 1.调用方法,执行微信退款
		 */
	
		double totalFee = order_amount_total;
			
			
		String merchantNumber = (String) orderDetails.get("order_no");
		// String wxTransactionNumber = "4200000218201901097243772476";
		// 本次金额
		double refund_fee = order_amount_total;


		System.out.println("refund_fee : "+refund_fee);
		System.out.println("merchantNumber : "+merchantNumber);
		
		// 申请退款
		Object setUrl = ClientCustomSSL.setUrl(merchantNumber, totalFee, refund_fee);
		System.out.println("result : " + setUrl.toString());
		Gson gson = new Gson();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap = gson.fromJson(setUrl.toString(), resultMap.getClass());
		System.out.println("resultMap : " + resultMap);
		String msg = (String) resultMap.get("msg");
//		/System.out.println("msg : "+msg);
		/*
		 * 2.调用方法,微信退款成功保存到订单表,并减掉上级佣金
		 */
		if (msg != null && msg.equals("success")) {
				// 保存退款信息
				System.out.println("退款成功");
				
				//修改订单状态
				Map<String,Object> ormap = new HashMap<String,Object>();
				ormap.put("o_id", map.get("o_id"));
				ormap.put("order_status", -1);
				int upo_status = orderService.updateOrderDetails(ormap);
				
				System.out.println("修改订单状态  :  "+upo_status);
				
				//删除核销码信息
				int delHX_status = orderService.delHXCode(HUtil.putInMap("order_id",map.get("o_id")));
				
				System.out.println("删除核销状态  :  "+delHX_status);
				
				for (Map<String, Object> map2 : orderDetailsList) {
					Map<String,Object> ogDetails = new HashMap<String,Object>();
					ogDetails.put("s_id", map2.get("supply_id"));
					//查询出商户最新的钱
					List<Map<String, Object>> shopMoneyLog = orderService.selectShopMoneyLog(ogDetails);
					Map<String, Object> shopMoneyLogDetails = shopMoneyLog.get(0);
		
					//最新的钱
					double sum = Double.parseDouble(shopMoneyLogDetails.get("sum").toString());
					//本单最后的结算价
					double ksss_price = Double.parseDouble(map2.get("ksss_price").toString()); 
					
					ogDetails.put("sum", sum-ksss_price);	
					ogDetails.put("money",map2.get("t_js_price"));	
					ogDetails.put("types", 1);	
					ogDetails.put("o_id",map.get("o_id"));	
					ogDetails.put("og_id",map2.get("order_goods_id"));	
					ogDetails.put("u_id",orderDetails.get("u_id"));	
					ogDetails.put("ss_price", map2.get("ss_price"));
					ogDetails.put("ksss_price", map2.get("ksss_price"));
					ogDetails.put("addtime", XVDateUtils.getCurrenTimeMillis());
					
					orderService.addShopMoney(ogDetails);
					
				}
				
				re_status = 1;
			} else if (msg != null && msg.equals("累计退款金额大于支付金额")) {
				System.out.println("累计退款金额大于支付金额");
				re_status = 3;
			} else if (msg != null && msg.equals("基本账户余额不足，请充值后重新发起")) {
				System.out.println("基本账户余额不足，请充值后重新发起");
				re_status = 4;
			} else if (msg != null && !msg.equals("success")) {
				// System.out.println("判断 : "+msg.equals("not exist"));
				System.out.println("退款失败");
				re_status = 0;
			}
			return re_status;
	}
	
	
	
	
	/**
	 * 查询订单详情
	 * */
	@ResponseBody
	@RequestMapping("/getCenterData.do")
	public Map<String,Object> getCenterData(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//待使用
		List<Map<String, Object>> dhx_list = orderService.selectAllHXOrderByHXStatus(HUtil.putInMap("code_sy",0));
		resultMap.put("dhx_count", dhx_list.size());
		//已核销
		List<Map<String, Object>> yhx_list = orderService.selectAllHXOrderByHXStatus(HUtil.putInMap("code_sy",1));
		resultMap.put("yhx_count", yhx_list.size());
		
		List<Map<String, Object>> allNowShopMoney = orderService.selectAllNowShopMoney();
		double shopMoney = 0.00;
		for (Map<String, Object> map2 : allNowShopMoney) {
			double sum = Double.parseDouble(map2.get("sum").toString());
			shopMoney = shopMoney +sum;
		}
		
		// 新方法，如果不需要四舍五入，可以使用RoundingMode.DOWN   需要 UP
        BigDecimal bg = new BigDecimal(shopMoney).setScale(2, RoundingMode.UP);
        shopMoney = bg.doubleValue();
		resultMap.put("shopMoney", shopMoney);
		
		
		return resultMap;
	}
	
	
	
	
	/**
	 * 查询订单详情
	 * */
	@ResponseBody
	@RequestMapping("/countOrderMoney.do")
	public int countOrderMoney(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		//查询出所有订单商品
		List<Map<String, Object>> allOrderGoods = orderService.selectAllOrderGoods();
		
		for (Map<String, Object> map2 : allOrderGoods) {
			//计算出税后价格
			double ss_price = Double.parseDouble(map2.get("ss_price").toString());
			
			double sl = 0.94;
			
			double ksss_price = ss_price * sl;
			
			// 新方法，如果不需要四舍五入，可以使用RoundingMode.DOWN   需要 UP
	        BigDecimal bg = new BigDecimal(ksss_price).setScale(2, RoundingMode.DOWN);
	        ksss_price = bg.doubleValue();
	        System.out.println("og_id : "+map2.get("order_goods_id") +"  t_js_price : "+map2.get("t_js_price")+ " ss_price : "+ss_price+ " ksss_price : "+ksss_price);
			
			orderService.updateOrderGoods(HUtil.putInMap("order_goods_id",map2.get("order_goods_id"),"ksss_price",ksss_price));
			
			
			//查询出商户最新的钱
			List<Map<String, Object>> shopMoneyLog = orderService.selectShopMoneyLog(HUtil.putInMap("s_id",map2.get("supply_id")));
			
			//最新的钱
			double sum = 0.00;
			if(shopMoneyLog.size() > 0) {
				Map<String, Object> shopMoneyLogDetails = shopMoneyLog.get(0);
				sum = Double.parseDouble(shopMoneyLogDetails.get("sum").toString());				
			}
			//本单扣税结算价
			//ksss_price
			//double ss_price = Double.parseDouble(shopMoneyLogDetails.get("ss_price").toString()); 
			Map<String,Object> ogDetails = new HashMap<String,Object>();
			ogDetails.put("s_id",map2.get("supply_id"));
			ogDetails.put("sum", sum+ksss_price);	
			ogDetails.put("money",map2.get("t_js_price"));	
			ogDetails.put("types", 0);	
			ogDetails.put("o_id",map2.get("order_id"));	
			ogDetails.put("og_id",map2.get("order_goods_id"));	
			ogDetails.put("u_id",map2.get("u_id"));	
			ogDetails.put("ss_price", map2.get("ss_price"));
			ogDetails.put("ksss_price", ksss_price);
			ogDetails.put("addtime", map2.get("addtime"));
			orderService.addShopMoney(ogDetails);
		}
		return 1;
	}
		
	
	
}